package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.cskaoyan.market.db.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.vo.WxSearchVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Autowired
    SecurityManager securityManager;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Override
    public WxSearchVo index() {
       MarketSearchHistoryExample searchHistoryExample = new MarketSearchHistoryExample();
       MarketSearchHistoryExample.Criteria searchHistoryExampleCriteria = searchHistoryExample.createCriteria();
       searchHistoryExampleCriteria.andDeletedEqualTo(true);
       searchHistoryExampleCriteria.andUserIdEqualTo(userId());
       MarketKeywordExample keywordExample = new MarketKeywordExample();
       MarketKeywordExample.Criteria criteria2 = keywordExample.createCriteria();
       criteria2.andIsHotEqualTo(true);
       List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(keywordExample);
       List<MarketSearchHistory> marketSearchHistoryList = marketSearchHistoryMapper.selectByExampleSelective(searchHistoryExample,MarketSearchHistory.Column.keyword);
        WxSearchVo wxSearchVo = new WxSearchVo();
        wxSearchVo.setDefaultKeyword(defaultKeyword());
        List<Map<String,String>> keywordList=new ArrayList<>();
        for (MarketSearchHistory marketSearchHistory : marketSearchHistoryList) {
            String keyword = marketSearchHistory.getKeyword();
            HashMap<String, String> map=new HashMap<>();
            map.put("keyword",keyword);
            keywordList.add(map);
        }
        wxSearchVo.setHistoryKeywordList(keywordList);
        wxSearchVo.setHotKeywordList(marketKeywords);
        return wxSearchVo;
    }

    @Override
    public List<String> helper(String keyword) {
        MarketKeywordExample marketKeywordExample=new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andKeywordLike("%"+keyword+"%");
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        List<String> keywordList=new ArrayList<>();
        for (MarketKeyword marketKeyword : marketKeywords) {
            keywordList.add(marketKeyword.getKeyword());
        }
        return keywordList;
    }

    @Override
    @Transactional
    public Integer clearHistory(MarketSearchHistory marketSearchHistory) {
        MarketSearchHistoryExample marketSearchHistoryExample=new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(userId());
        marketSearchHistory.setUpdateTime(LocalDateTime.now());
        Integer i=marketSearchHistoryMapper.updateByExampleSelective(marketSearchHistory,marketSearchHistoryExample);
        return i;
    }

    private Integer userId() {
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria userExampleCriteria = marketUserExample.createCriteria();
        userExampleCriteria.andUsernameEqualTo(user.getUsername());
        MarketUser marketUser = marketUserMapper.selectByExample(marketUserExample).get(0);
        Integer userId = marketUser.getId();
        return userId;
    }

    private Object defaultKeyword() {
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        MarketKeyword marketKeyword = marketKeywordMapper.selectByExample(keywordExample).get(0);
        return marketKeyword;
    }
}
